<template>
  <div id="input">
    <input
      type="text"
      placeholder="按回车键新增代办 ~ ~"
      v-model="itemname"
      @keyup.enter="inputenter"
    />
  </div>
</template>

<script>
export default {
  data() {
    return {
      itemname: '',
    };
  },
  props: ['selectdate'],
  methods: {
    async getItems() {
      let datetime;
      if (!this.selectdate) {
        let now = new Date();
        datetime = `${now.getFullYear()}-${
          now.getMonth() + 1
        }-${now.getDate()}`;
      } else {
        datetime = this.selectdate;
      }
      let res = await this.axios.get('/item/search', {
        params: { key: '', datetime },
      });
      let itemlist = res.data.data;

      itemlist.n.items.forEach((e) => {
        if (Number(e.ingstate)) {
          this.iteming = e;
          let time = e.time;
          let h = parseInt(time / 3600);
          let m = parseInt((time / 60) % 60);
          let s = Math.ceil(time % 60);
          let hours = h < 10 ? '0' + h : h;
          let minute = m < 10 ? '0' + m : m;
          let second = s > 59 ? 59 : s < 10 ? '0' + s : s;
          this.h = hours;
          this.m = minute;
          this.s = second;
        }
      });
      this.$store.commit('GetItemList', {
        y: itemlist.y.items,
        n: itemlist.n.items,
      });
      let { data } = await this.axios.get('/user/getuser');
      let user = data.data[0];
      this.$store.commit('GetUser', user);
    },
    async inputenter() {
      let datetime;
      if (!this.selectdate) {
        let now = new Date();
        datetime = `${now.getFullYear()}-${
          now.getMonth() + 1
        }-${now.getDate()}`;
      } else {
        datetime = this.selectdate;
      }
      let res = await this.axios.get('/item/add', {
        params: { name: this.itemname, datetime },
      });
      if (res.data.status == 200) {
        alert(res.data.data);
        this.itemname = '';
        this.getItems();
      }
    },
  },
};
</script>

<style scoped>
#input {
  width: 100%;
  height: 100%;
}
input {
  text-indent: 1em;
  width: 100%;
  height: 100%;
  border: none;
  background: none;
  outline: none;
  font-size: 18px;
}
input::placeholder {
  color: #f4a460;
}
</style>
